package hutech.ksv.dao;

import hutech.ksv.entity.QuanAnEntity;

import java.io.Serializable;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import javax.sql.DataSource;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

@Repository
public class QuanAnDao implements Serializable {

	@Autowired
	DataSource dataSource;

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	// Test connection
	public Connection getConnection() {
		try {
			Class.forName("com.mysql.jdbc.Driver");
			return DriverManager.getConnection(
					"jdbc:mysql://localhost:3306/mydb", "root", "mysql@123");
		} catch (ClassNotFoundException | SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return null;
	}

	// Insert Quan An
	public void insertQuanAn(QuanAnEntity quanan) {
		String query = "INSERT INTO `QuanAn` (`DiaChiQA`, `MoTaQA`, `TrangThaiDuyetQA`, `TenTKCQA`, `TaiKhoanQTV_TenTKQTV`, `HinhAnhQA`) VALUES (?,?,?,?,?,?)";
		Connection conn = null;
		PreparedStatement preparedStatement = null;

		try {
			conn = dataSource.getConnection();
			preparedStatement = conn.prepareStatement(query);

			preparedStatement.setString(1, quanan.getDiaChiQA());
			preparedStatement.setString(2, quanan.getMoTaQA());
			preparedStatement.setBoolean(3, quanan.isTrangThaiDuyetQA());
			preparedStatement.setString(4, quanan.getTenTKCQA());
			preparedStatement.setString(5, quanan.getTaiKhoanQTV());
			preparedStatement.setString(6, quanan.getHinhAnhQA());

			preparedStatement.execute();
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			try {
				if (preparedStatement != null) {
					preparedStatement.close();
				}
				if (conn != null) {
					conn.close();
				}
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
	}

	// Update Quan An
	public void updateQuanAn(QuanAnEntity quanan) {
		String query = "UPDATE `QuanAn` " + "SET `DiaChiQA`=?,"
				+ "`MoTaQA`=?,`TrangThaiDuyetQA`=?,"
				+ "`TenTKCQA`=?,`TaiKhoanQTV_TenTKQTV`=?, `HinhAnhQA`=?" + "WHERE `MaQA`=?";
		Connection conn = null;
		PreparedStatement preparedStatement = null;

		try {
			conn = dataSource.getConnection();
			preparedStatement = conn.prepareStatement(query);

			preparedStatement.setInt(7, quanan.getMaQA());
			preparedStatement.setString(1, quanan.getDiaChiQA());
			preparedStatement.setString(2, quanan.getMoTaQA());
			preparedStatement.setBoolean(3, quanan.isTrangThaiDuyetQA());
			preparedStatement.setString(4, quanan.getTenTKCQA());
			preparedStatement.setString(5, quanan.getTaiKhoanQTV());
			preparedStatement.setString(6, quanan.getHinhAnhQA());

			preparedStatement.execute();
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			try {
				if (preparedStatement != null) {
					preparedStatement.close();
				}
				if (conn != null) {
					conn.close();
				}
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
	}

	// Load thong tin quan an
	public List<QuanAnEntity> loadThongTinQA() {
		String query = "SELECT * FROM `QuanAn`";
		List<QuanAnEntity> listThongTinQA = new ArrayList<QuanAnEntity>();

		Connection conn = null;
		PreparedStatement preparedStatement = null;

		try {
			conn = dataSource.getConnection();
			preparedStatement = conn.prepareStatement(query);
			ResultSet rs = preparedStatement.executeQuery();
			while (rs.next()) {
				listThongTinQA.add(new QuanAnEntity(rs.getInt(1), rs.getString(2), rs.getString(3), 
													rs.getBoolean(4), rs.getString(5), rs.getString(6), rs.getString(7)));
			}
			rs.close();
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			try {
				if (preparedStatement != null) {
					preparedStatement.close();
				}
				if (conn != null) {
					conn.close();
				}
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
		return listThongTinQA;
	}

	/*// Load danh sach Quan An tim theo tenQA
	public List<QuanAnEntity> loadDSQATheoTen(String tenQA) {
		List<QuanAnEntity> listDS = new ArrayList<QuanAnEntity>();
		String query = "SELECT * FROM `QuanAn` WHERE `TenQA`=?";
		Connection conn = null;
		PreparedStatement preparedStatement = null;
		try {

			conn = dataSource.getConnection();
			preparedStatement = conn.prepareStatement(query);
			preparedStatement.setString(1, tenQA);

			ResultSet rs = preparedStatement.executeQuery();

			while (rs.next()) {
				QuanAnEntity ds = new QuanAnEntity();
				ds.setMaQA(rs.getInt("MaQA"));
				ds.setDiaChiQA(rs.getString("DiaChiQA"));
				ds.setMoTaQA(rs.getString("MoTaQA"));
				ds.setTrangThaiDuyetQA(rs.getBoolean("TrangThaiDuyetQA"));
				ds.setTenTKCQA(rs.getString("TenTKCQA"));
				ds.setTaiKhoanQTV(rs.getString("TaiKhoanQTV"));
				listDS.add(ds);
			}
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			try {
				if (preparedStatement != null) {
					preparedStatement.close();
				}
				if (conn != null) {
					conn.close();
				}
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
		return listDS;
	}*/

	// Load danh sach Quan An tim theo dia chi
	public List<QuanAnEntity> loadDSQATheoDC(QuanAnEntity quanan) {
		String diaChi = quanan.getDiaChiQA();
		List<QuanAnEntity> listDS = new ArrayList<QuanAnEntity>();
		String query = "SELECT * FROM `QuanAn` WHERE `DiaChiQA`=?";
		Connection conn = null;
		PreparedStatement preparedStatement = null;
		try {

			conn = dataSource.getConnection();
			preparedStatement = conn.prepareStatement(query);
			preparedStatement.setString(1, diaChi);

			ResultSet rs = preparedStatement.executeQuery();

			while (rs.next()) {
				listDS.add(new QuanAnEntity(rs.getInt(1), rs.getString(2), rs.getString(3), 
						rs.getBoolean(4), rs.getString(5), rs.getString(6), rs.getString(7)));
			}
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			try {
				if (preparedStatement != null) {
					preparedStatement.close();
				}
				if (conn != null) {
					conn.close();
				}
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
		return listDS;
	}
}
